﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;

namespace Ramparts
{
    class CannonPhase : Scene
    {
        SpriteSheet cannonSheet;
        Texture2D cannonTexture;
        Vector2 mousePos;

        public CannonPhase(SceneManager sceneManager)
        {
            this.sceneManager = sceneManager;

        }

        public override bool initScene()
        {
            base.initScene();
            cannonTexture = sceneManager.rampart.Content.Load<Texture2D>("Content\\object_tiles");
            cannonSheet = new SpriteSheet(cannonTexture);
            cannonSheet.AddSourceSprite(1,
                new Rectangle(40, 0, 40, 40));

            mousePos = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);

            viewportRect = new Rectangle(0, 0,
                                    sceneManager.rampart.graphics.GraphicsDevice.Viewport.Width,
                                    sceneManager.rampart.graphics.GraphicsDevice.Viewport.Height);
            camera.ManualCamera = false;
            camera.Position = new Vector2(-1 * viewportRect.Width / 2, -1 * viewportRect.Height / 2);
            return true;
            
        }

        public override void drawScene()
        {
            base.drawScene();
            sceneManager.world.Draw();
            drawComponents();

            Rectangle sourceRect = cannonSheet.GetRectangle(1);
            sceneManager.rampart.spriteBatch.Draw(
                  cannonSheet.Texture,
                  sceneManager.world.buildingLayer.onTile(mousePos) * 20,  
                  sourceRect,
                  Color.White,
                  0,
                  new Vector2(0,0),
                  1,
                  SpriteEffects.None,
                  1);


            sceneManager.rampart.spriteBatch.End();
        }

        protected override void processKeyboardEvents(GameTime gameTime)
        {
            base.processKeyboardEvents(gameTime);
        
        }


        protected override void processMouseEvents(GameTime gameTime)
        {
            base.processMouseEvents(gameTime);
            mousePos = new Vector2(currentMouseState.X, currentMouseState.Y);

            // TODO add a timer to this if statement if you run out of time
            if (!sceneManager.world.CanPlaceCannon(0))
            {
                BattlePhase newScene = new BattlePhase(sceneManager);
                newScene.initScene();
                sceneManager.AddScene(newScene);
            }

            if (currentMouseState.LeftButton == ButtonState.Released && previousMouseState.LeftButton == ButtonState.Pressed)
            {
                if (viewportRect.Contains((int)mousePos.X, (int)mousePos.Y))
                {
                    Vector2 tilePos = sceneManager.world.wallLayer.onTile(mousePos);
                    if (sceneManager.world.PlaceCannon(0, mousePos))
                    {
                        Console.WriteLine("Placed Cannon");
                    }
                }
            }

            previousMouseState = currentMouseState;
            
        
        }

        public override void Update(GameTime gameTime)
        {

            base.Update(gameTime);
             
            
        }

    }
}
